package view;

import controller.*;
import model.*;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;


public class EnterGameJPanel extends JPanel {
    private Controller controller;
    public JPanel cardPanel;
    public JPanel westPanel;
    public JPanel eastPanel;
    public JPanel southPanel;
    private JLabel westLabel;
    private JLabel eastLabel;
    private JLabel southLabel;
    public JButton jaoFen1Button;
    public JButton jaoFen2Button;
    public JButton jaoFen3Button;
    public JButton noJaoButton;
    public JTextField jTextField;
    public JTextField eastTextField;
    public JTextField westTextField;
    public JLabel soreJLabel;
    public JLabel westSoreJLabel;
    public JLabel eastSoreJLabel;
    public JTextField soutDeZhuField;
    public JTextField westDeZhuField;
    public JTextField eastDeZhuField;
    public JButton noPlay;
    public JButton yesPlay;
    public JButton inDication;
    public JLabel noPlayLabel;
    public ArrayList<JLabel> cardList;
    public JButton openCards;


    public EnterGameJPanel(Controller controller) {
        this.controller = controller;
        cardList = new ArrayList<>();
        setPreferredSize(new Dimension(1200, 800));
        setLayout(new BorderLayout());
        //叫1分
        jaoFen1Button = new JButton("1分");
        jaoFen1Button.setBounds(410, 70, 80, 30);
        jaoFen1Button.addActionListener(new ScoreButtonListener(1, soreJLabel));
        //叫2分
        jaoFen2Button = new JButton("2分");
        jaoFen2Button.setBounds(510, 70, 80, 30);
        jaoFen2Button.addActionListener(new ScoreButtonListener(2, soreJLabel));
        //叫3分
        jaoFen3Button = new JButton("3分");
        jaoFen3Button.setBounds(600, 70, 80, 30);
        jaoFen3Button.addActionListener(new ScoreButtonListener(3, soreJLabel));
        //不叫分
        noJaoButton = new JButton("不叫");
        noJaoButton.setBounds(690, 70, 80, 30);
        noJaoButton.addActionListener(new ScoreButtonListener(0, soreJLabel));

        // 创建西、东、南三个方位的面板
        westPanel = new JPanel();
        eastPanel = new JPanel();
        southPanel = new JPanel();
        jTextField = new JTextField();
        westTextField = new JTextField();
        eastTextField = new JTextField();
        //西东的叫分标识
        westSoreJLabel = new JLabel();
        eastSoreJLabel = new JLabel();

        westTextField.setEditable(false);
        eastTextField.setEditable(false);
        jTextField.setEditable(false);

        jTextField.setBounds(550, 20, 80, 20);
        eastTextField.setBounds(80, 230, 80, 20);
        westTextField.setBounds(23, 230, 80, 20);
        //明牌按钮
        openCards = new JButton("明牌");
        openCards.addActionListener(new OpenCardsLister());
        openCards.setBounds(230, 35, 80, 30);
        openCards.setVisible(false);
        southPanel.add(openCards);
        openCards.setSelected(false);
        //地主标识框
        westDeZhuField = new JTextField();
        eastDeZhuField = new JTextField();
        soutDeZhuField = new JTextField();

        westDeZhuField.setEditable(false);
        eastDeZhuField.setEditable(false);
        soutDeZhuField.setEditable(false);

        soutDeZhuField.setBounds(650, 10, 30, 50);
        eastDeZhuField.setBounds(90, 230, 30, 50);
        westDeZhuField.setBounds(20, 230, 30, 50);

        noPlay = new JButton("不出");
        yesPlay = new JButton("出牌");
        yesPlay.addMouseListener(new userPlayCardsLister(controller));

        inDication = new JButton("提示");
        noPlayLabel = new JLabel();
        noPlayLabel.setBounds(410, 45, 60, 30);
        noPlayLabel.setFont(new Font("黑体", Font.BOLD, 28));
        noPlayLabel.setForeground(Color.white);
        noPlay.setBounds(480, 45, 60, 30);
        yesPlay.setBounds(580, 45, 60, 30);
        inDication.setBounds(680, 45, 60, 30);
        noPlay.addActionListener(new PlayButtonLister(noPlayLabel));


        noPlay.setVisible(false);
        yesPlay.setVisible(false);
        inDication.setVisible(false);


        southPanel.add(noJaoButton);
        southPanel.add(jaoFen1Button);
        southPanel.add(jaoFen2Button);
        southPanel.add(jaoFen3Button);
        southPanel.add(jTextField);
        southPanel.add(noPlay);
        southPanel.add(yesPlay);
        southPanel.add(inDication);
        southPanel.add(noPlayLabel);


        westPanel.setPreferredSize(new Dimension(200, 800));
        westSoreJLabel.setBounds(10, 10, 180, 30);
        westSoreJLabel.setForeground(Color.white);
        westSoreJLabel.setFont(new Font("黑体", Font.BOLD, 30));
        westPanel.add(westTextField);
        westPanel.add(westSoreJLabel);


        westPanel.add(westDeZhuField);
        southPanel.add(soutDeZhuField);
        eastPanel.add(eastDeZhuField);


        eastPanel.setPreferredSize(new Dimension(200, 800));
        eastSoreJLabel.setBounds(70, 10, 180, 30);
        eastSoreJLabel.setForeground(Color.white);
        eastSoreJLabel.setFont(new Font("黑体", Font.BOLD, 30));
        eastPanel.add(eastTextField);
        eastPanel.add(eastSoreJLabel);


        southPanel.setPreferredSize(new Dimension(1200, 300));
        //将面板设置透明
        westPanel.setOpaque(false);
        eastPanel.setOpaque(false);
        southPanel.setOpaque(false);
//        southPanel.setBackground(Color.BLACK);
        westLabel = new JLabel("椰羊");
        westLabel.setBounds(50, 100, 100, 50);
        westLabel.setFont(new Font("黑体", Font.BOLD, 30)); // 设置字体样式和大小
        westLabel.setForeground(Color.WHITE);
        westPanel.add(westLabel);
        westPanel.setLayout(null);


        eastLabel = new JLabel("堂主");
        eastLabel.setBounds(50, 100, 100, 50); // 设置位置和大小
        eastLabel.setFont(new Font("黑体", Font.BOLD, 30)); // 设置字体样式和大小
        eastLabel.setForeground(Color.WHITE);
        eastPanel.add(eastLabel);
        eastPanel.setLayout(null);


        soreJLabel = new JLabel();
        soreJLabel.setBounds(400, 10, 200, 50);
        soreJLabel.setFont(new Font("黑体", Font.BOLD, 30));
        soreJLabel.setForeground(Color.WHITE);


        southLabel = new JLabel("屑狐狸");
        southLabel.setBounds(80, 30, 200, 50);
        southLabel.setFont(new Font("黑体", Font.BOLD, 30)); // 设置字体样式和大小
        southLabel.setForeground(Color.WHITE);
        southPanel.add(southLabel);
        southPanel.add(soreJLabel);
        southPanel.setLayout(null);


        cardPanel = new JPanel();
        cardPanel.setOpaque(false);
        cardPanel.setLayout(new FlowLayout());
        add(southPanel, BorderLayout.SOUTH);
        add(eastPanel, BorderLayout.EAST);
        add(westPanel, BorderLayout.WEST);
        add(cardPanel, BorderLayout.NORTH);

    }


    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(DataSource.zhuoMianBackGround, 0, 0, 1200, 800, null);


        //西电脑玩家的手牌
        if (westDeZhuField.getText().equals("地主")) {

            // 将底牌添加到玩家手牌中
            for (Card card : controller.underPlayer.list) {
                controller.getWestPcPlayer().list.add(card);
            }
            // 从底牌列表中移除已添加的牌
            if (controller.getWestPcPlayer().list.removeAll(controller.underPlayer.list)) {
                controller.getWestPcPlayer().list.addAll(controller.underPlayer.list);
            }

        }
        //画西边玩家
        Collections.sort(controller.getWestPcPlayer().list);
        for (int i = 0; i < controller.getWestPcPlayer().list.size(); i++) {
            if (UI.enterGameJFrame.enterGameJPanel.openCards.isSelected()) {
                g.drawImage(controller.getWestPcPlayer().list.get(i).getFontImage(), 110, 20 + i * 20, 50, 80, null);
            } else {
                g.drawImage(controller.getWestPcPlayer().list.get(i).getBackImage(), 110, 20 + i * 20, 50, 80, null);
            }
        }

        //手牌添加
        //人类玩家的手牌
        if (soutDeZhuField.getText().equals("地主")) {


//            // 将底牌添加到玩家手牌中
            for (Card card : controller.underPlayer.list) {

                controller.getPersonPlayer().list.add(card);

            }
//            if ( controller.underPlayer.list.size() > 0) {
//                controller.getPersonPlayer().list.add( controller.underPlayer.list.remove(0));
//            }

            // 从底牌列表中移除已添加的牌
            /*// 设置地主牌的点击状态为可点击
            for (Card card : controller.underPlayer.list) {
                card.setClicked(true);
            }*/

            if (controller.getPersonPlayer().list.removeAll(controller.underPlayer.list)) {
                controller.getPersonPlayer().list.addAll(controller.underPlayer.list);
            }
        }
//        cardLis();
        Collections.sort(controller.getPersonPlayer().list);
//        if (controller.judge){
        for (int i = 0; i < controller.getPersonPlayer().list.size(); i++) {
            Card card = controller.getPersonPlayer().list.get(i);

            if (card.isClicked()) {
                g.drawImage(card.getFontImage(), 300 + i * 40, 600, 50, 80, null);
            } else {
                g.drawImage(card.getFontImage(), 300 + i * 40, 650, 50, 80, null);
            }
        }
//        }


        //东电脑玩家的手牌
        if (eastDeZhuField.getText().equals("地主")) {
            // 将底牌添加到玩家手牌中
            for (Card card : controller.underPlayer.list) {
                controller.getEastPcPlayer().list.add(card);
            }
            // 从底牌列表中移除已添加的牌
            if (controller.getEastPcPlayer().list.removeAll(controller.underPlayer.list)) {
                controller.getEastPcPlayer().list.addAll(controller.underPlayer.list);
            }
        }

        Collections.sort(controller.getEastPcPlayer().list);
        //画东边玩家
        for (int i = 0; i < controller.getEastPcPlayer().list.size(); i++) {
            if (UI.enterGameJFrame.enterGameJPanel.openCards.isSelected()) {
                g.drawImage(controller.getEastPcPlayer().list.get(i).getFontImage(), 1000, 20 + i * 20, 50, 80, null);
            } else {
                g.drawImage(controller.getEastPcPlayer().list.get(i).getBackImage(), 1000, 20 + i * 20, 50, 80, null);
            }
        }

        for (int i = 0; i < controller.underPlayer.list.size(); i++) {
            if (westDeZhuField.getText().equals("地主") || westDeZhuField.getText().equals("东家") || eastDeZhuField.getText().equals("东家") || soutDeZhuField.getText().equals("东家") || eastDeZhuField.getText().equals("地主") || soutDeZhuField.getText().equals("地主")) {
                g.drawImage(controller.underPlayer.list.get(i).getFontImage(), 500 + i * 60, 20, 50, 80, null);
            } else {
                g.drawImage(controller.underPlayer.list.get(i).getBackImage(), 500 + i * 60, 20, 50, 80, null);
            }
        }


        for (int i = 0; i < controller.getAllList().size(); i++) {
            g.drawImage(controller.getAllList().get(i).getBackImage(), 500 + i * 5, 300, 50, 80, null);
        }
        for (int i = 0; i < controller.deskList.size(); i++) {
            g.drawImage(controller.deskList.get(i).getFontImage(), 540 + i * 30, 250, 50, 80, null);
        }

        g.drawImage(DataSource.dizhu, 10, 300, 80, 80, null);
        g.drawImage(DataSource.dizhu1, 1100, 300, 80, 80, null);
        g.drawImage(DataSource.dizhu2, 80, 600, 80, 80, null);
        repaint();

    }
//    ArrayList<Card> outCardList = new ArrayList<>();

    //    //    //添加监听方法
    public void cardLis() {

        Collections.sort(controller.getPersonPlayer().list);
        for (int i = 0; i < controller.getPersonPlayer().list.size(); i++) {
            Card card = controller.getPersonPlayer().list.get(i);
            card.removeMouseListener(controller.mouseLis);
            card.addMouseListener(controller.mouseLis);
            this.setComponentZOrder(card, 0);
        }
    }
//
//        for (JLabel jLabel : cardList) {
//            jLabel.addMouseListener(controller.mouseLis);
//            this.add(jLabel);
//            this.setComponentZOrder(jLabel,0);
//        }
//
//    }
//
//    public void cardClear(){
//        for (JLabel jLabel : cardList) {
//            jLabel.removeMouseListener(controller.mouseLis);
//            this.remove(jLabel);
//        }
//        cardList.clear();
//    }


}










